
package shop.maytinh.controller;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.Transformers;

import shop.maytinh.util.HibernateUtil;
import shop.maytinh.model.*;

 

public class CommentManager extends HibernateUtil {
	
	static List<Comment> listcomment;
	public Comment comment = null; 
	
	public static List<Comment> getCommentByProduct(Integer id) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();			   
        try {
        	session.beginTransaction();
            String hql = "FROM Comment E WHERE E.product.id = :ex";
            Query query = session.createQuery(hql);
            query.setParameter("ex",id);
            List employees = query.list();
            //Product pd = new Product();
            listcomment = (List<Comment>)query.setMaxResults(5).list();      
            return listcomment;
        } catch (Exception e) {           
           return null;
        }		
    }
	
	public Comment add(Comment comment) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(comment);
		session.getTransaction().commit();
		return comment;
	}
}


